home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / old-stream / itoa.cc < prev    next >
C/C++ Source or Header  |  1992-01-17  |  5KB  |  252 lines

  1. /* 
  2. Copyright (C) 1990 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  
  21. */
  22.  
  23. #ifdef __GNUG__
  24. #pragma implementation
  25. #endif
  26. #include <builtin.h>
  27. #include <AllocRing.h>
  28.  
  29. extern AllocRing _libgxx_fmtq;
  30.  
  31. char* itoa(long x, int base, int width)
  32. {
  33.   int wrksiz;
  34.   if (base == 10)
  35.     wrksiz = width + 13;
  36.   else
  37.     wrksiz = (BITS(long) + 1) / lg(base) + 1 + width + 1;
  38.  
  39.   char* fmtbase = (char *) _libgxx_fmtq.alloc(wrksiz);
  40.   char* e = fmtbase + wrksiz - 1;
  41.   char* s = e;
  42.   *--s = 0;
  43.   char sgn = 0;
  44.  
  45.   if (x == 0)
  46.     *--s = '0';
  47.   else
  48.   {
  49.     unsigned int z;
  50.     if (x < 0)
  51.     {
  52.       sgn = '-';
  53.       z = -x;
  54.     }
  55.     else
  56.       z = x;
  57.     while (z != 0)
  58.     {
  59.       char ch = char(z % base);
  60.       z = z / base;
  61.       if (ch >= 10)
  62.         ch += 'a' - 10;
  63.       else
  64.         ch += '0';
  65.       *--s = ch;
  66.     }
  67.   }
  68.  
  69.   if (sgn) *--s = sgn;
  70.   int w = e - s - 1;
  71.   while (w++ < width)
  72.     *--s = ' ';
  73.   return s;
  74. }
  75.  
  76. char* itoa(unsigned long x,  int base, int width)
  77. {
  78.   int wrksiz;
  79.   if (base == 10)
  80.     wrksiz = width + 13;
  81.   else
  82.     wrksiz = (BITS(long) + 1) / lg(base) + 1 + width + 1;
  83.  
  84.   char* fmtbase = (char *) _libgxx_fmtq.alloc(wrksiz);
  85.   char* e = fmtbase + wrksiz - 1;
  86.   char* s = e;
  87.   *--s = 0;
  88.  
  89.   if (x == 0)
  90.     *--s = '0';
  91.   else
  92.   {
  93.     unsigned int b = base;
  94.     while (x != 0)
  95.     {
  96.       char ch = char(x % b);
  97.       x = x / b;
  98.       if (ch >= 10)
  99.         ch += 'a' - 10;
  100.       else
  101.         ch += '0';
  102.       *--s = ch;
  103.     }
  104.   }
  105.  
  106.   int w = e - s - 1;
  107.   while (w++ < width)
  108.     *--s = ' ';
  109.   return s;
  110. }
  111.  
  112. #ifdef __GNUG__
  113. #ifndef VMS
  114. char* itoa(long long x, int base, int width)
  115. {
  116.   int wrksiz;
  117.   if (base == 10)
  118.     wrksiz = width + 23;
  119.   else
  120.     wrksiz = (BITS(long long) + 1) / lg(base) + 1 + width + 1;
  121.  
  122.   char* fmtbase = (char *) _libgxx_fmtq.alloc(wrksiz);
  123.   char* e = fmtbase + wrksiz - 1;
  124.   char* s = e;
  125.   *--s = 0;
  126.   char sgn = 0;
  127.  
  128.   if (x == 0)
  129.     *--s = '0';
  130.   else
  131.   {
  132.     long long z;
  133.     if (x < 0)
  134.     {
  135.       sgn = '-';
  136.       z = -x;
  137.     }
  138.     else
  139.       z = x;
  140.     while (z != 0)
  141.     {
  142.       char ch = char(z % base);
  143.       z = z / base;
  144.       if (ch >= 10)
  145.         ch += 'a' - 10;
  146.       else
  147.         ch += '0';
  148.       *--s = ch;
  149.     }
  150.   }
  151.  
  152.   if (sgn) *--s = sgn;
  153.   int w = e - s - 1;
  154.   while (w++ < width)
  155.     *--s = ' ';
  156.   return s;
  157. }
  158.  
  159. char* itoa(unsigned long long x,  int base, int width)
  160. {
  161.   int wrksiz;
  162.   if (base == 10)
  163.     wrksiz = width + 23;
  164.   else
  165.     wrksiz = (BITS(long long) + 1) / lg(base) + 1 + width + 1;
  166.  
  167.   char* fmtbase = (char *) _libgxx_fmtq.alloc(wrksiz);
  168.   char* e = fmtbase + wrksiz - 1;
  169.   char* s = e;
  170.   *--s = 0;
  171.  
  172.   if (x == 0)
  173.     *--s = '0';
  174.   else
  175.   {
  176.     unsigned int b = base;
  177.     while (x != 0)
  178.     {
  179.       char ch = char(x % b);
  180.       x = x / b;
  181.       if (ch >= 10)
  182.         ch += 'a' - 10;
  183.       else
  184.         ch += '0';
  185.       *--s = ch;
  186.     }
  187.   }
  188.  
  189.   int w = e - s - 1;
  190.   while (w++ < width)
  191.     *--s = ' ';
  192.   return s;
  193. }
  194. #endif
  195. #endif
  196.  
  197. char* hex(long i, int width)
  198. {
  199.   return itoa(i, 16, width);
  200. }
  201.  
  202. char* oct(long i, int width)
  203. {
  204.   return itoa(i, 8, width);
  205. }
  206.  
  207. char* dec(long i, int width)
  208. {
  209.   return itoa(i, 10, width);
  210. }
  211.  
  212. char* hex(unsigned long i, int width)
  213. {
  214.   return itoa(i, 16, width);
  215. }
  216.  
  217. char* oct(unsigned long i, int width)
  218. {
  219.   return itoa(i, 8, width);
  220. }
  221.  
  222. char* dec(unsigned long i, int width)
  223. {
  224.   return itoa(i, 10, width);
  225. }
  226.  
  227. // The following functions used to be inline in builtin.h, but
  228. // the definitions there clashed with those in iostream/stream.C.
  229. // Since people aren't supposed to be using these functions in
  230. // any case, there is no reason to make them inline.
  231.  
  232. char* hex(int x, int width = 0) { return hex(long(x), width); }
  233. char* hex(short x, int width = 0) { return hex(long(x), width); }
  234. char* hex(unsigned int x, int width = 0) 
  235. { return hex((unsigned long)(x), width); }
  236. char* hex(unsigned short x, int width = 0) 
  237. { return hex((unsigned long)(x), width); }
  238.  
  239. char* oct(int x, int width = 0) { return oct(long(x), width); }
  240. char* oct(short x, int width = 0) { return oct(long(x), width); }
  241. char* oct(unsigned int x, int width = 0) 
  242. { return oct((unsigned long)(x), width); }
  243. char* oct(unsigned short x, int width = 0) 
  244. { return oct((unsigned long)(x), width); }
  245.  
  246. char* dec(int x, int width = 0) { return dec(long(x), width); }
  247. char* dec(short x, int width = 0) { return dec(long(x), width); }
  248. char* dec(unsigned int x, int width = 0) 
  249. { return dec((unsigned long)(x), width); }
  250. char* dec(unsigned short x, int width = 0) 
  251. { return dec((unsigned long)(x), width); }
  252.